using System;
using System.Text.RegularExpressions;
using BitBoard = System.UInt64;
using Piece = System.Int32;
//---------------------------------------------------------------------------//
//																			 //
//			r  n  C  h  e  s  s  B  o  a  r  d  C  o  n  t  r  o  l			 //
//			=======================================================			 //
//						  (c) 2003 Chris R. Chapman							 //
//																			 //
//	Version:		1.0	Full												 //
//  Date:			May 2003												 //
//  Author:			Chris R. Chapman (chris@chapmanconsulting.ca)			 //
//  Description:	A chessboard GUI written in 100% .Net/C#, featuring		 //
//					a resizable board with adjustable colors and configurable//
//					images for pieces, as well as events for capturing player//
//					movement and illegal moves.								 //
//																			 //
//																			 //
// LICENSING:		Can be freely distributed and used as long as the		 //
//					following criteria are met:								 //
//																			 //
//					1) All copyright notices are kept intact;				 //
//					2) Product is not for commercial resale;				 //
//					3) Product is NOT used in conjunction with ANY GPLed or  //
//					   FSF software.  I cannot abide by their philosophies.  //
//					4) Credit for the control to me is made in some			 //
//					   conspicuous fashion, like an About box.				 //
//---------------------------------------------------------------------------//
namespace RNChessBoardCommonTypes
{   
    /// <summary>
	/// This class provides methods and functions that assist in the conversion of square indexing between
	/// Standard Algebraic Notation and virtual chessboard array indexing.
	/// </summary>
	public class SquareIndexingTools
	{
        public static string[] AlgebraicBoardIndex = new string[64]
        {
            "A8", "B8", "C8", "D8", "E8", "F8", "G8", "H8",
            "A7", "B7", "C7", "D7", "E7", "F7", "G7", "H7",
            "A6", "B6", "C6", "D6", "E6", "F6", "G6", "H6",
            "A5", "B5", "C5", "D5", "E5", "F5", "G5", "H5",
            "A4", "B4", "C4", "D4", "E4", "F4", "G4", "H4",
            "A3", "B3", "C3", "D3", "E3", "F3", "G3", "H3",
            "A2", "B2", "C2", "D2", "E2", "F2", "G2", "H2",
            "A1", "B1", "C1", "D1", "E1", "F1", "G1", "H1"
        };

		public SquareIndexingTools()
		{}
		
		/// <summary>
		/// Converts a Standard Algebraic Notation coordinate representing a square on a chessboard into
		/// a corresponding integer value (0-63).
		/// </summary>
		/// <param name="algebraicSquareNotation">Standard Algebraic Notation coordinate of the square.</param>
		/// <returns>Integer (0-63) for a square on a chessboard.  It is assumed that the board is 
		/// numbered from 0 in the top-left corner to 63 in the bottom-right corner.
		/// </returns>
		public static int ConvertAlgebraicToIndex(string algebraicSquareNotation)
		{
			if(!Regex.Match(algebraicSquareNotation,"^[A-Ha-h][1-8]$").Success)
			{
				throw new ArgumentException("Algebraic notation for square coordinate is invalid.");
			}

			int squareRank = (int)algebraicSquareNotation[0] - 65;
			int squareFile = (8 - int.Parse(algebraicSquareNotation[1].ToString())) * 8;
			
			return squareFile + squareRank;
		}

		/// <summary>
		/// Converts a square index (0-63) representing a square on a chessboard to a corresponding algebraic notation.
		/// </summary>
		/// <param name="squareIndex">Integer (0-63) for a square on a chessboard.  It is assumed
		/// that the board is numbered from 0 in the top-left corner to 63 in the bottom-right corner.
		/// </param>
		/// <returns>Standard Algebraic Notation coordinate of the square.</returns>
		/// <remarks>
		/// Throws ArgumentException if squareIndex is outside of the allowable range of 0-63.
		/// </remarks>
		public static string ConvertIndexToAlgebraic(int squareIndex)
		{
			if(squareIndex > 63 || squareIndex < 0)
			{
				throw new ArgumentException("Square index must be in the range of 0-63");
			}

			int rank = (squareIndex / 8) + 1;	// gets our rank index (1-8)
			int file = (squareIndex & 7);	// gets our file index (A-H)

			char baseChar = (char)(file + 65);
			string notation = baseChar.ToString() + (9 - rank).ToString();

			return notation;
		}
	}

}
